package com.alks.function.controller;

import com.alks.common.aop.ApiRestController;
import com.alks.common.aop.WebLogger;
import com.alks.common.data.thread.UserIdThread;
import com.alks.common.utils.DateUtils;
import com.alks.common.utils.excelUtils.ExcelUtils;
import com.alks.entity.data.entity.PcBeforeLastTime;
import com.alks.entity.data.response.ResponseInfo;
import com.alks.entity.exception.ServiceErrorException;
import com.alks.function.data.dto.PcProdStatus2Dto;
import com.alks.function.data.request.PcProdStatus2Request;
import com.alks.function.data.request.chengKong.LotNoToStyleRequest;
import com.alks.function.service.PcProdStatus2Service;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@ApiRestController("/PcProdStatus2")
@Api(tags = "生产动态分析")
@Slf4j
public class PcProdStatus2Controller {

    @Autowired
    private PcProdStatus2Service pcProdStatus2Service;

    @WebLogger
    @ApiOperation("生产动态查询")
    @PostMapping("/getProductionDynamicQuery")
    public ResponseInfo getProductionDynamicQuery(@RequestBody PcProdStatus2Request request){
        return pcProdStatus2Service.getProductionDynamicQueryInfo(request);
    }

    @WebLogger
    @ApiOperation("时程门槛定义")
    @GetMapping("/getTime")
    public ResponseInfo getTime(){
        return pcProdStatus2Service.getTimeDeFine();
    }

    @ApiOperation("修改时程门槛定义")
    @PutMapping("/update")
    @WebLogger
    public ResponseInfo update(@RequestBody PcBeforeLastTime pcBeforeLastTime){
        return pcProdStatus2Service.updateTimeDeFine(pcBeforeLastTime);
    }

    @ApiOperation("导出")
    @PutMapping("/export")
    @WebLogger
    public void export(@RequestBody PcProdStatus2Request request, HttpServletResponse response){
        String templetName = "生产动态表模版.xls";
        String excelName = "生产动态表"+ DateUtils.dateTimeNow()+".xls";
        request.setPageSize(-1);
        request.setPageNum(-1);
        List<PcProdStatus2Dto> exportList = pcProdStatus2Service.export(request);
        Map<String, Object> map = new HashMap<>(2);
        map.put("exportDate", LocalDate.now().format(DateTimeFormatter.ofPattern("yyyy/MM/dd")));
        try {
            ExcelUtils.creatByTemplate(response,templetName,excelName, exportList,map);
        } catch (IOException e) {
            throw new ServiceErrorException("导出失败");
        }
    }

    @ApiOperation("生成生产动态表")
    @PostMapping("/GenerateProductionDynamics")
    @WebLogger
    public ResponseInfo GenerateProductionDynamics(){
        return pcProdStatus2Service.GenerateProductionDynamics(UserIdThread.get().getCompanyId(), UserIdThread.get().getLoginName());
    }

    @ApiOperation("标注指令已结束")
    @PostMapping("/labelFlag")
    @WebLogger
    public ResponseInfo labelFlag(@RequestParam String lotNo){
        return pcProdStatus2Service.labelFlag(lotNo);
    }

    @ApiOperation("查询标注结束指令-取消标注")
    @PostMapping("/cancelLabelFlag")
    @WebLogger
    public ResponseInfo cancelLabelFlag(@RequestParam String lotNo){
        return pcProdStatus2Service.cancelLabelFlag(lotNo);
    }

    @ApiOperation("核算时间")
    @GetMapping("/ReturnTime")
    @WebLogger
    public ResponseInfo ReturnTime(){
        return pcProdStatus2Service.ReturnTime();
    }

    @ApiOperation("查询标注结束指令")
    @PostMapping("/getLotNos")
    @WebLogger
    public ResponseInfo getLotNos(@RequestBody LotNoToStyleRequest request){
        return pcProdStatus2Service.getLotNos(request);
    }
}
